
bt.module({builder: function(bt) {
    j$ = jQuery;

    var _calculateTextLength = function() {
        var obj = j$("form#send-message-form textarea");
        j$(".count-left").html(500 - obj.val().length);
    };

    var _afterLoadMessageList = function() {
        j$(".message-list .list-history .item").click(function() {
            j$(location).attr("href", j$(this).data("href"));
        });

        j$(".message-list .list-history .latest-msg").each(function() {
            j$(this).ThreeDots({
                max_rows: 1,
                whole_word: false
            });
        });

    };

    var _initLoadMoreEvent = function() {
        j$(".message-list #load-more").click(function() {
            var _this = this;
            j$('#ajax-more').show();
            j$('#load-more .icon').hide();
            j$('#ajax-more').css("visibility", "visible");

            j$.ajax({
                url: j$(this).data("url"),
                data: "start=" + j$(_this).data("start")
                    + "&count=" + j$(_this).data("count"),
                success: function(resp) {
                    if(resp) {
                        j$(".list-history").append(resp);
                    } else {
                        j$("#load-more").slideUp();
                    }
                }
            }).done(function() {
                j$(_this).remove();
                _afterLoadMessageList();
                _initLoadMoreEvent();
            });
        });

        j$(".message-talk #load-more").click(function() {
            var _this = this;
            j$('#ajax-more').show();
            j$('#load-more .icon').hide();
            j$('#ajax-more').css("visibility", "visible");

            j$.ajax({
                url: j$(this).data("url"),
                data: "oldest_id=" + j$(".talk-history .item").last().data("id"),
                success: function(resp) {
                    if(resp) {
                        j$(".talk-history").append(resp);
                    } else {
                        j$("#load-more").slideUp();
                    }
                }
            }).done(function() {
                j$(_this).remove();
                _initLoadMoreEvent();
            });
        });
    };

    j$(document).ready(function() {
        j$(".send-message-btn").click(function() {
            var form = j$("form#send-message-form");
            if(j$.trim(form.find("textarea").val())) {
                form.submit();
            } else {
                alert("不能发送空私信哦");
                form.find("textarea").val("");
            }
        });

        j$("form#send-message-form textarea").bind("input", function() {
            _calculateTextLength();
        });

        j$("form#send-message-form textarea").bind("keydown", function() {
            _calculateTextLength();
        });

        j$("form#send-message-form textarea").bind("cut", function() {
            _calculateTextLength();
        });

        j$("form#send-message-form textarea").bind("propertychange", function() {
            _calculateTextLength();
        });
        _afterLoadMessageList();
        _initLoadMoreEvent();
    });

}});
